﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

// ReSharper disable once CheckNamespace
namespace com.nm.Helper
{
    /// <summary>
    /// 数学和数字有关系的方法
    /// </summary>
    public static class MathHelper
    {
        /// <summary>
        /// 是否在两个数字之间（min &lt; value &lt; max），如果min大于max会自动交换
        /// </summary>
        /// <param name="theValue"></param>
        /// <param name="min">小值（不含）</param>
        /// <param name="max">大值（不含）</param>
        /// <returns></returns>
        public static bool IsBetween(this int theValue, int min, int max)
        {
            if (min > max)
            {
                Swap(ref min, ref max);
            }
            return theValue > min && theValue < max;
        }
        /// <summary>
        /// 返回在给定的大小值之间的数值，不会修改theValue
        /// </summary>
        /// <param name="theValue"></param>
        /// <param name="min"></param>
        /// <param name="max"></param>
        /// <returns></returns>
        public static int MakeInRange(this int theValue, int min, int max)
        {
            if (min > max)
            {
                Swap(ref min, ref max);
            }
            return theValue < min
                ? min
                : (theValue > max ? max : theValue);
        }
        /// <summary>
        /// 交换数值
        /// </summary>
        /// <param name="v1"></param>
        /// <param name="v2"></param>
        public static void Swap(ref int v1, ref int v2)
        {
            var tmp = v1;
            v1 = v2;
            v2 = tmp;
        }


        /// <summary>
        /// 实现数据的四舍五入法
        /// </summary>
        /// <param name="v">要进行处理的数据</param>
        /// <param name="x">保留的小数位数</param>
        /// <returns>四舍五入后的结果</returns>
        public static double Rounding(double value, int digit)
        {
            double vt = Math.Pow(10, digit);
            //1.乘以倍数 + 0.5
            double vx = value * vt + 0.5;
            //2.向下取整
            double temp = Math.Floor(vx);
            //3.再除以倍数
            return (temp / vt);
        }
    }
}
